package com.internship.javawebinternship.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.internship.javawebinternship.entity.*;
import com.internship.javawebinternship.myUtils.MD5Encrypt;
import com.internship.javawebinternship.myUtils.ReadExcel;
import com.internship.javawebinternship.myUtils.SendEmail;
import com.internship.javawebinternship.repository.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountRepository accountRepository;
    @Autowired
    private TutorInfoRepository tutorInfoRepository;
    @Autowired
    private StudentInfoRepository studentInfoRepository;
    @Autowired
    private OtherEmployeeInfoRepository otherEmployeeInfoRepository;
    @Autowired
    private TutorClassesRepository tutorClassesRepository;

    @PostMapping("logIn")//登录
    public Object logIn(@RequestBody String frontEnd,
                        HttpServletRequest request) throws Exception {
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        Map<String, Object> result = new HashMap<>();

        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String password = receiveJSON.getString("password");
        String email = receiveJSON.getString("email");
        String no = receiveJSON.getString("no");

        password = MD5Encrypt.encryptMD5(password);//使用MD5加密算法，将输入的密码加密
        Object res = accountRepository.findByEmailOrStuempnoAndPassword(email, no, password);//使用输入的账号信息，与加密后的密码，在数据库中寻找有无相关记录

        //Map对象的键是String类型的，而值因为被定义为最上层的java对象Object对象，
        // 而所有的下级对象都隶属于Object对象，所以可以传任何类型的值在里面，

        HttpSession session1 = request.getSession(false);
        if (session1 != null) {
            result.put("result", "0");
            result.put("msg", "您已经登录了哦，请不要重复登录");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        //当数据库中有相关记录时
        if (!((ArrayList<?>) res).isEmpty()) {
            String authority = ((Account) ((ArrayList<?>) res).get(0)).getAuthority();//获得用户的权限信息
            String name = ((Account) ((ArrayList<?>) res).get(0)).getName();//获得用户名
            no = ((Account) ((ArrayList<?>) res).get(0)).getStuempno();
            boolean isNew = false;
            if (((Account) ((ArrayList<?>) res).get(0)).getEmail() == null || ((Account) ((ArrayList<?>) res).get(0)).getEmail().isEmpty() || ((Account) ((ArrayList<?>) res).get(0)).getEmail().equals(""))
                isNew = true;
            switch (authority) {//根据权限的不同，将登录成功的信息，以及不同的提示放在map集合中
                case "0":
                    result.put("result", "1");
                    result.put("msg", "管理员：" + name + "，您好！");
                    break;
                case "1":
                    result.put("result", "1");
                    result.put("msg", "辅导员：" + name + "，您好！");
                    break;
                case "2":
                    result.put("result", "1");
                    result.put("msg", "同学：" + name + "，您好！");
                    break;
                case "3":
                    result.put("result", "1");
                    result.put("msg", "教职工：" + name + "，您好！");
                    break;
            }
            result.put("authority", authority);
            result.put("name", name);
            result.put("isNew", isNew);

            HttpSession session = request.getSession(true);
            session.setMaxInactiveInterval(300);
            session.setAttribute("name", name);
            session.setAttribute("authority", authority);
            session.setAttribute("no", no);

            Account account = (Account) ((ArrayList<?>) res).get(0);
            account.setSessionid(session.getId());
            accountRepository.save(account);

        } else {//找不到相关记录，将登录失败的信息，提示信息放在map集合中
            result.put("result", "0");
            result.put("msg", "用户名或密码错误");
        }
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("forget")//忘记密码
    public Object forget(@RequestBody String frontEnd,
                         HttpServletRequest request) {
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);

        Map<String, Object> result = new HashMap<>();

        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String email = receiveJSON.getString("email");

        HttpSession session = request.getSession(true);
        session.setMaxInactiveInterval(150);

        Object res = accountRepository.findByEmail(email);//使用输入的信息，在数据库中查找相关记录
        //如果有相关记录
        if (!((ArrayList<?>) res).isEmpty()) {
            result.put("result", "1");//将查找结果置1
            String authority = ((Account) ((ArrayList<?>) res).get(0)).getAuthority();//获得用户的权限信息
            String name = ((Account) ((ArrayList<?>) res).get(0)).getName();//获得用户的姓名
            int code = (int) ((Math.random() * 9 + 1) * 100000);//生成6位随机验证码
            session.setAttribute("code", String.valueOf(code));
            session.setAttribute("no", ((Account) ((ArrayList<?>) res).get(0)).getStuempno());

            //构造电子邮件的正文
            String emailContent = "<h3>尊敬的用户 " + name + "：<br>您好！<br>您本次申请的验证码是：</h3> <h1>" + code + "</h1><br><h4 align='right'>GLUT校园疫情防控管理系统</h4>";
            System.out.println(emailContent);
            //调用方法，发送电子邮件
            SendEmail sendEmail = new SendEmail(email, "GLUT校园疫情防控管理系统通知", emailContent);
            //调用start方法，执行线程
            sendEmail.start();
        } else {//没有相关记录
            result.put("result", "0");//查找结果置0
        }
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("verify")//验证用户输入的验证码与生成的验证码是否一致
    public Object verify(HttpServletRequest request,
                         @RequestBody String frontEnd) {

        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        String code;
        try {
            code = (String) session.getAttribute("code");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您的会话已超时，或您的访问非法，请重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);

        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String inputCode = receiveJSON.getString("inputCode");

        if (code.equals(inputCode)) {
            result.put("result", "1");//一致，置1
        } else {
            result.put("result", "0");//不一致，置0
        }

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("resetPassword")//重设密码
    public Object resetPassword(HttpServletRequest request,
                                @RequestBody String frontEnd) throws Exception {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您的会话已超时，或您的访问非法，请重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String newPassword = receiveJSON.getString("newPassword");
        String reNewPassword = receiveJSON.getString("reNewPassword");


        if (!newPassword.equals(reNewPassword)) {
            result.put("result", "0");//结果置0
            result.put("msg", "两次密码输入不同");
            JSONObject object = new JSONObject(result);
            return object;
        }

        newPassword = MD5Encrypt.encryptMD5(newPassword);//使用MD5加密用户输入的新密码

        //尝试为用户更新密码
        try {
            Account accountFromDb = accountRepository.findByStuempno(no).get(0);
            accountFromDb.setPassword(newPassword);

            accountRepository.save(accountFromDb);
        } catch (Exception e) {//如果遇到错误
            result.put("result", "0");//结果置0
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        session.invalidate();
        result.put("result", "1");//修改成功，结果置1
        result.put("msg", "密码修改成功");

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("showInfo")//根据所给用户信息编号，展示用户的所有数据（不包括密码）
    public Object showInfo(HttpServletRequest request,
                           @RequestBody String frontEnd) {

        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String no = receiveJSON.getString("no");

        Account accountFromDb = accountRepository.findByStuempno(no).get(0);
        //获取用户信息
        String stuEmpNo = accountFromDb.getStuempno();
        String name = accountFromDb.getName();
        String email = accountFromDb.getEmail();
        String gender = accountFromDb.getGender();
        String authority = accountFromDb.getAuthority();

        //将用户信息放在map集合中
        result.put("result", "1");
        result.put("name", name);
        result.put("email", email);
        result.put("no", stuEmpNo);
        result.put("gender", gender);
        result.put("authority", authority);

        switch (authority) {
            case "1":
                TutorInfo tutorInfo = tutorInfoRepository.findById(no).get();
                result.put("college", tutorInfo.getCollege());
                break;
            case "2":
                StudentInfo studentInfo = studentInfoRepository.findById(no).get();
                result.put("college", studentInfo.getCollege());
                result.put("profession", studentInfo.getProfession());
                result.put("grade", studentInfo.getGrade());
                result.put("classes", studentInfo.getClasses());
                break;
            case "3":
                OtherEmployeeInfo otherEmployeeInfo = otherEmployeeInfoRepository.findById(no).get();
                result.put("department", otherEmployeeInfo.getDepartment());
                break;
        }

        //将map集合转换为json，提交给前端
        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("showSelfInfo")//根据用户信息编号，展示当前登录用户的所有数据（不包括密码）
    public Object showSelfInfo(HttpServletRequest request) {

        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Account accountFromDb = accountRepository.findByStuempno(no).get(0);
        //获取用户信息
        String stuEmpNo = accountFromDb.getStuempno();
        String name = accountFromDb.getName();
        String email = accountFromDb.getEmail();
        String gender = accountFromDb.getGender();
        String authority = accountFromDb.getAuthority();

        //将用户信息放在map集合中
        result.put("result", "1");
        result.put("name", name);
        result.put("email", email);
        result.put("no", stuEmpNo);
        result.put("gender", gender);
        result.put("authority", authority);

        switch (authority) {
            case "1":
                TutorInfo tutorInfo = tutorInfoRepository.findById(no).get();
                result.put("college", tutorInfo.getCollege());
                break;
            case "2":
                StudentInfo studentInfo = studentInfoRepository.findById(no).get();
                result.put("college", studentInfo.getCollege());
                result.put("profession", studentInfo.getProfession());
                result.put("grade", studentInfo.getGrade());
                result.put("classes", studentInfo.getClasses());
                break;
            case "3":
                OtherEmployeeInfo otherEmployeeInfo = otherEmployeeInfoRepository.findById(no).get();
                result.put("department", otherEmployeeInfo.getDepartment());
                break;
        }

        //将map集合转换为json，提交给前端
        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("showInfoList")
    public Object showInfoList(@RequestBody String frontEnd,
                               HttpServletRequest request) {
        HttpSession session = request.getSession(false);

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> result2 = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (!session.getAttribute("authority").equals("0")) {
                result2.put("result", "0");
                result2.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result2);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String authority = receiveJSON.getString("authority");

        int count = accountRepository.countByAuthority(authority);
        if (count == 0) {
            result2.put("result", "0");
            result2.put("msg", "当前数据库中没有所选类型的用户");
            JSONObject object = new JSONObject(result2);
            return object;
        }

        try {
            List<Account> account;
            if (authority == null)
                account = accountRepository.findAllUser();
            else
                account = accountRepository.findByAuthorityOrderByAuthorityAsc(authority);

            result2.put("result", "1");
            result2.put("data", account);
        } catch (IndexOutOfBoundsException e) {
            JSONObject object = new JSONObject(result2);
            return object;
        }

        JSONObject object = new JSONObject(result2);
        return object;
    }

    @PostMapping("setModify")//根据用户信息编号，使用前端传入的数据，修改该用户的信息
    public Object setModify(@RequestBody String frontEnd,
                            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no = receiveJSON.getString("no");
        String name = receiveJSON.getString("name");
        String email = receiveJSON.getString("email");
        String password = receiveJSON.getString("password");
        String gender = receiveJSON.getString("gender");
        String authority = receiveJSON.getString("authority");
        String college = receiveJSON.getString("college");
        String grade = receiveJSON.getString("grade");
        String profession = receiveJSON.getString("profession");
        String classes = receiveJSON.getString("classes");
        String department = receiveJSON.getString("department");

        try {
            Account account = accountRepository.findByStuempno(no).get(0);

            if (email != null)
                account.setEmail(email);
            if (!name.equals(""))
                account.setName(name);
            if (password != null) {
                password = MD5Encrypt.encryptMD5(password);
                account.setPassword(password);
            }
            if (!gender.equals(""))
                account.setGender(gender);
            if (!authority.equals(""))
                account.setAuthority(authority);

            accountRepository.save(account);

            switch (authority) {
                case "1":
                    TutorInfo tutorInfo = tutorInfoRepository.findById(no).get();
                    if (!college.equals(""))
                        tutorInfo.setCollege(college);
                    tutorInfoRepository.save(tutorInfo);
                    break;
                case "2":
                    StudentInfo studentInfo = studentInfoRepository.findById(no).get();
                    if (!college.equals(""))
                        studentInfo.setCollege(college);
                    if (!profession.equals(""))
                        studentInfo.setProfession(profession);
                    if (!grade.equals(""))
                        studentInfo.setGrade(grade);
                    if (!classes.equals(""))
                        studentInfo.setClasses(classes);
                    studentInfoRepository.save(studentInfo);
                    break;
                case "3":
                    OtherEmployeeInfo otherEmployeeInfo = otherEmployeeInfoRepository.findById(no).get();
                    if (!department.equals(""))
                        otherEmployeeInfo.setDepartment(department);
                    otherEmployeeInfoRepository.save(otherEmployeeInfo);
                    break;
            }

        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        result.put("result", "1");
        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("getTutorManage")
    public Object getTutorManage(HttpServletRequest request,
                                 @RequestBody String frontEnd) {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String empNo = receiveJSON.getString("empNo");

        session.setAttribute("empNo", empNo);

        Map<String, Object> result2 = new HashMap<>();
        int j = 0;
        try {
            List<TutorClasses> tutorClasses;
            tutorClasses = tutorClassesRepository.findByEmpno(empNo);
            result2.put("result", "1");
            result2.put("data", tutorClasses);
        } catch (IndexOutOfBoundsException e) {
            JSONObject object = new JSONObject(result2);
            return object;
        }

        JSONObject object = new JSONObject(result2);
        return object;
    }

    @PostMapping("setTutorManage")
    public Object setTutorManage(HttpServletRequest request,
                                 @RequestBody String frontEnd) {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String empNo = receiveJSON.getString("empNo");
        String afterModify = receiveJSON.getString("afterModify");

        try {
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
            if (empNo.equals("")) {
                empNo = (String) session.getAttribute("empNo");
                session.removeAttribute("empNo");
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject recv = JSONObject.parseObject(afterModify);

        if (tutorClassesRepository.deleteByEmpno(empNo) == 0) {
            result.put("result", "0");
            result.put("msg", "辅导员旧信息删除失败");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            int i = 0;
            while (true) {
                JSONObject out = (JSONObject) recv.get(i++);
                if (out.get("result").equals("0"))
                    break;
                tutorClassesRepository.UpdateTutorInfo(empNo, (String) out.get("college"), (String) out.get("profession"),
                        (String) out.get("grade"), (String) out.get("classes"));
            }
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "辅导员信息更新失败，请重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        result.put("result", "1");
        result.put("msg", "辅导员信息更新成功");
        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("verifyEmail")//验证用户输入的新Email
    public Object verifyEmail(@RequestBody String frontEnd,
                              HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        HttpSession session = request.getSession(false);

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String email = receiveJSON.getString("email");

        if (accountRepository.countByEmail(email) != 0) {
            result.put("result", "0");
            result.put("msg", "该Email已被使用");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Account accountFromDb = accountRepository.findByStuempno(no).get(0);

        String name = accountFromDb.getName();//获得用户的姓名
        int code = (int) ((Math.random() * 9 + 1) * 100000);//生成6位随机验证码
        session.setAttribute("email", email);
        session.setAttribute("code", String.valueOf(code));//将验证码放session中
        String emailContent = "<h3>尊敬的用户 " + name + "：<br>您好！<br>您本次申请的验证码是：</h3> <h1>" + code + "</h1><br><h4 align='right'>GLUT校园疫情防控管理系统</h4>";
        System.out.println(emailContent);
        //调用方法，发送电子邮件
            SendEmail sendEmail = new SendEmail(email, "GLUT校园疫情防控管理系统通知", emailContent);
        //调用start方法，执行线程
            sendEmail.start();
        result.put("result", "1");//将运行结果置为1

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("setNewEmail")//根据用户信息编号，为用户更新Email
    public Object setNewEmail(HttpServletRequest request,
                              @RequestBody String frontEnd) {
        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        String code;
        try {
            no = (String) session.getAttribute("no");
            code = (String) session.getAttribute("code");
            session.removeAttribute("code");

        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String inputCode = receiveJSON.getString("inputCode");
        if (!code.equals(inputCode)){
            result.put("result", "0");
            result.put("msg", "验证码不正确");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String email = (String) session.getAttribute("email");
        session.removeAttribute("email");

        try {
            Account accountFromDb = accountRepository.findByStuempno(no).get(0);
            accountFromDb.setEmail(email);
            accountRepository.save(accountFromDb);
        } catch (Exception e) {
            result.put("result", "0");//出错，将结果置0
            JSONObject object = new JSONObject(result);
            return object;
        }

        result.put("result", "1");//将运行结果置为1

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("setNewPassword")//根据用户信息编号，为用户更新密码
    public Object setNewPassword(@RequestBody String frontEnd,
                                 HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String oldPassword = receiveJSON.getString("oldPassword");
        String password = receiveJSON.getString("password");
        String RePassword = receiveJSON.getString("rePassword");

        Account accountFromDb = accountRepository.findByStuempno(no).get(0);
        String databasePassword = accountFromDb.getPassword();
        oldPassword = MD5Encrypt.encryptMD5(oldPassword);//使用MD5加密用户输入的新密码
        if (oldPassword.equals(databasePassword)) {
            if (password.equals(RePassword)) {
                try {
                    password = MD5Encrypt.encryptMD5(password);//使用MD5加密用户输入的新密码
                    accountFromDb.setPassword(password);
                    accountRepository.save(accountFromDb);
                } catch (Exception e) {
                    result.put("result", "0");//出错，将结果置0
                    result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
                    JSONObject object = new JSONObject(result);
                    return object;
                }
            } else {
                result.put("result", "0");//出错，将结果置0
                result.put("msg", "两次密码输入不同");
                JSONObject object = new JSONObject(result);
                return object;
            }

            result.put("result", "1");//将运行结果置为1
            result.put("msg", "密码修改成功，请重新登录");
            session.invalidate();
            JSONObject object = new JSONObject(result);
            return object;
        } else {
            result.put("result", "0");//出错，将结果置0
            result.put("msg", "旧密码不正确！");
            JSONObject object = new JSONObject(result);
            return object;
        }
    }

    @PostMapping("addNewUserSingle")
    public Object addNewUserSingle(@RequestBody String frontEnd,
                                   HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String adminNo;
        try {
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
            adminNo = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no = receiveJSON.getString("no");
        String name = receiveJSON.getString("name");
        String password = receiveJSON.getString("password");
        String gender = receiveJSON.getString("gender");
        String authority = receiveJSON.getString("authority");
        String email = receiveJSON.getString("email");
        if (email == null)
            email = "";
        String profession = receiveJSON.getString("profession");
        String grade = receiveJSON.getString("grade");
        String classes = receiveJSON.getString("classes");
        String department = receiveJSON.getString("department");
        String college = receiveJSON.getString("college");
        String date = receiveJSON.getString("date");
        String adminPassword = receiveJSON.getString("adminPassword");

        if (date == null || date.equals("")) {
            Date DATE = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//将日期精确到具体的时分秒
            date = formatter.format(DATE);
        }

        try {
            Object res = accountRepository.findByStuempno(no);
            if (!((ArrayList<?>) res).isEmpty()) {
                result.put("result", "0");
                result.put("msg", "编号" + no + "已经注册");
                result.put("name", name);
                result.put("date", date);
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (Exception e) {
            result.put("result", "0");
            result.put("date", date);
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            Object res = accountRepository.findByEmail(email);
            if (!((ArrayList<?>) res).isEmpty()) {
                result.put("result", "0");
                result.put("msg", "Email" + email + "已经注册");
                result.put("name", name);
                result.put("date", date);
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (Exception e) {
            result.put("result", "0");
            result.put("date", date);
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        if (authority.equals("0") || authority.equals("管理员")) {
            try {
                authority = "0";
                Account account = accountRepository.findByStuempno(adminNo).get(0);
                if (!MD5Encrypt.encryptMD5(adminPassword).equals(account.getPassword())) {
                    result.put("result", "0");
                    result.put("date", date);
                    result.put("msg", "管理员密码不正确");
                    JSONObject object = new JSONObject(result);
                    return object;
                }
            } catch (IndexOutOfBoundsException e) {
                result.put("result", "0");
                result.put("date", date);
                result.put("msg", "管理员密码不正确");
                JSONObject object = new JSONObject(result);
                return object;
            } catch (Exception e) {
                result.put("result", "0");
                result.put("date", date);
                result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
                JSONObject object = new JSONObject(result);
                return object;
            }

        } else {
            try {
                switch (authority) {
                    case "辅导员":
                    case "1":
                        authority = "1";
                        break;
                    case "学生":
                    case "2":
                        authority = "2";
                        break;
                    case "其他职工":
                    case "3":
                        authority = "3";
                        break;
                    default:
                        result.put("msg", "用户编号 " + no + " 所对应的“权限”数据不合法！");
                        throw new IllegalArgumentException();
                }
                if (no.length() <= 6) {
                    result.put("msg", "用户编号 " + no + "所对应的学号或工号长度需大于6位！");
                    throw new IllegalArgumentException();
                }
                if (!gender.equals("男") && !gender.equals("女")) {
                    result.put("msg", "用户编号 " + no + "所对应的“性别”数据不合法！");
                    throw new IllegalArgumentException();
                }
                switch (authority) {
                    case "1":
                        if (college.equals("")) {
                            result.put("msg", "辅导员编号 " + no + "所对应的一项或多项数据不合法！");
                            throw new IllegalArgumentException();
                        }
                        break;
                    case "2":
                        if (profession.equals("") || grade.equals("") || classes.equals("") || college.equals("")) {
                            result.put("msg", "学生编号 " + no + "所对应的一项或多项数据不合法！");
                            throw new IllegalArgumentException();
                        }
                        break;
                    case "3":
                        if (department.equals("")) {
                            result.put("msg", "其他职工编号 " + no + "所对应的一项或多项数据不合法！");
                            throw new IllegalArgumentException();
                        }
                        break;
                }
            } catch (IllegalArgumentException e) {
                result.put("result", "0");
                result.put("name", name);
                result.put("date", date);
                JSONObject object = new JSONObject(result);
                return object;
            }
        }

        try {
            Account account = new Account();
            if (!email.equals(""))
                account.setEmail(email);
            account.setStuempno(no);
            account.setName(name);
            if (password.equals("")) {
                password = no.substring(no.length() - 6);
            }
            password = MD5Encrypt.encryptMD5(password);//使用MD5加密用户输入的新密码
            account.setPassword(password);
            account.setGender(gender);
            account.setAuthority(authority);
            account.setRegistertime(date);

            accountRepository.save(account);

            switch (authority) {
                case "0":
                    break;
                case "1":
                    TutorInfo tutorInfo = new TutorInfo();
                    tutorInfo.setEmpno(no);
                    tutorInfo.setCollege(college);
                    tutorInfoRepository.save(tutorInfo);
                    break;
                case "2":
                    StudentInfo studentInfo = new StudentInfo();
                    studentInfo.setStuno(no);
                    studentInfo.setCollege(college);
                    studentInfo.setProfession(profession);
                    studentInfo.setGrade(grade);
                    studentInfo.setClasses(classes);
                    studentInfoRepository.save(studentInfo);
                    break;
                case "3":
                    OtherEmployeeInfo otherEmployeeInfo = new OtherEmployeeInfo();
                    otherEmployeeInfo.setEmpno(no);
                    otherEmployeeInfo.setDepartment(department);
                    otherEmployeeInfoRepository.save(otherEmployeeInfo);
                    break;
            }

        } catch (Exception e) {
            result.put("result", "0");
            result.put("name", name);
            result.put("date", date);
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        result.put("result", "1");
        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("addNewUserBulk")
    public Object addNewUserBulk(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String excelFileName = "";
        try {
            excelFileName = (String) session.getAttribute("fileName");
            session.removeAttribute("fileName");
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        File excel = new File("excelUploaded/" + excelFileName);

        excelFileName = excel.getAbsolutePath();
        List<Map<String, String>> list = ReadExcel.read(excelFileName);

        Date DATE = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//将日期精确到具体的时分秒
        String date = formatter.format(DATE);

        int i = 0;
        int j = 0;
        boolean error = false;
        String errMsg = "";

        for (Map<String, String> map : list) {
            if (i < 5) {
                i++;
                continue;
            }
            if (map.get("authority").equals("") && map.get("name").equals(""))
                break;

            Map<String, Object> temp = new HashMap<>();
            temp.put("no", map.get("no"));
            temp.put("name", map.get("name"));
            temp.put("password", map.get("password"));
            temp.put("gender", map.get("gender"));
            temp.put("authority", map.get("authority"));
            temp.put("email", map.get("email"));
            temp.put("profession", map.get("profession"));
            temp.put("grade", map.get("grade"));
            temp.put("classes", map.get("classes"));
            temp.put("department", map.get("department"));
            temp.put("college", map.get("college"));
            temp.put("date", date);
            temp.put("adminPassword", "");

            Object json = addNewUserSingle(JSON.toJSONString(temp), request);

            String resultReturn = JSON.toJSONString(json);

            if (resultReturn.contains("result\":\"0")) {
                result.put("result", "0");
                String[] temp1 = resultReturn.split("\"msg\":\"");
                errMsg = temp1[1].substring(0, temp1[1].indexOf("\""));
                error = true;
                result.put("msg", "有用户创建失败，已为您回滚本次操作，请查看错误信息、检查Excel表格并重试");
                result.put("err", errMsg);
                break;
            } else {
                j++;
            }
        }

        if (error) {
            for (int a = 0; a < j; a++) {
                accountRepository.deleteByRegistertime(date);
            }
        }

        if (result.get("result") == null || !result.get("result").equals("0"))
            result.put("result", "1");

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("deleteUserSingle")
    public Object deleteUserSingle(@RequestBody String frontEnd,
                                   HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String no = receiveJSON.getString("no");

        if (accountRepository.findByStuempno(no).isEmpty()) {
            result.put("result", "0");
            result.put("msg", "数据库中无此用户");
            JSONObject object = new JSONObject(result);
            return object;
        }

        if (accountRepository.findByStuempno(no).get(0).getSessionid() != null) {
            result.put("result", "1");
            result.put("msg", "该用户已登录，已将其强制下线并删除");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            accountRepository.deleteByStuempno(no);
            result.put("result", "1");
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "删除失败，请重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("deleteUserBulkAsk")
    public Object deleteUserBulkAsk(@RequestBody String frontEnd,
                                    HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();

        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String authority = receiveJSON.getString("authority");
        String profession = receiveJSON.getString("profession");
        String grade = receiveJSON.getString("grade");
        String classes = receiveJSON.getString("classes");
        String department = receiveJSON.getString("department");
        String college = receiveJSON.getString("college");

        try {
            session.setAttribute("authority111", authority);
            switch (authority) {
                case "1":
                    if (tutorInfoRepository.findTutorGroup(college).isEmpty()) {
                        result.put("result", "0");
                        result.put("msg", "所指定的辅导员用户组不存在，请修改后重试");
                    } else {
                        result.put("result", "1");
                        String msg = "将删除";
                        if (!college.equals("")) {
                            msg = msg + " " + college + " ";
                            session.setAttribute("college", college);
                        }
                        msg = msg + "所有辅导员用户，请输入您的密码以确认操作！";
                        result.put("msg", msg);
                    }
                    break;
                case "2":
                    if (studentInfoRepository.findStudentGroup(grade, classes, profession, college).isEmpty()) {
                        result.put("result", "0");
                        result.put("msg", "所指定的学生用户组不存在，请修改后重试");
                    } else {
                        result.put("result", "1");
                        String msg = "将删除";
                        if (!college.equals("")) {
                            msg = msg + " " + college + " ";
                            session.setAttribute("college", college);
                        }
                        if (!profession.equals("")) {
                            msg = msg + " " + profession + "专业 ";
                            session.setAttribute("profession", profession);
                        }
                        if (!grade.equals("")) {
                            msg = msg + " " + grade + "级 ";
                            session.setAttribute("grade", grade);
                        }
                        if (!classes.equals("")) {
                            msg = msg + " " + classes + "班 ";
                            session.setAttribute("classes", classes);
                        }
                        msg = msg + "所有学生用户，请输入您的密码以确认操作！";
                        result.put("msg", msg);
                    }
                    break;
                case "3":
                    if (otherEmployeeInfoRepository.findEmployeeGroup(department).isEmpty()) {
                        result.put("result", "0");
                        result.put("msg", "所指定的其他职工用户组不存在，请修改后重试");
                    } else {
                        result.put("result", "1");
                        String msg = "将删除";
                        if (!department.equals("")) {
                            msg = msg + " " + department + "部门 ";
                            session.setAttribute("department", department);
                        }
                        msg = msg + "所有其他职工用户，请输入您的密码以确认操作！";
                        result.put("msg", msg);
                    }
                    break;
                default:
                    throw new IllegalArgumentException();
            }
        } catch (IllegalArgumentException e) {
            result.clear();
            result.put("result", "0");
            result.put("msg", "输入的参数不合法，请重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端

        } catch (Exception e) {
            result.clear();
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("deleteUserBulkOperation")
    public Object deleteUserBulkOperation(@RequestBody String frontEnd,
                                          HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
            if (!session.getAttribute("authority").equals("0")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String adminPassword = receiveJSON.getString("adminPassword");

        try {
            Account account = accountRepository.findByStuempno(no).get(0);
            if (!MD5Encrypt.encryptMD5(adminPassword).equals(account.getPassword())) {
                throw new Exception();
            }
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "管理员密码验证失败，请重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String authority = (String) session.getAttribute("authority111");
        session.removeAttribute("authority111");
        String college = (String) session.getAttribute("college");
        String grade = (String) session.getAttribute("grade");
        String classes = (String) session.getAttribute("classes");
        String profession = (String) session.getAttribute("profession");
        String department = (String) session.getAttribute("department");

        try {
            switch (authority) {
                case "1":
                    try {
                        while (true) {
                            TutorInfo tutorInfo = tutorInfoRepository.findTutorGroup(college).get(0);
                            accountRepository.deleteByStuempno(tutorInfo.getEmpno());
                        }
                    } catch (IndexOutOfBoundsException e) {
                        result.put("result", "1");
                        result.put("msg", "删除成功");
                        session.removeAttribute("college");
                        session.removeAttribute("grade");
                        JSONObject object = new JSONObject(result);
                        return object;
                    }
                case "2":
                    try {
                        while (true) {
                            StudentInfo studentInfo = studentInfoRepository.findStudentGroup(grade, classes, profession, college).get(0);
                            accountRepository.deleteByStuempno(studentInfo.getStuno());
                        }
                    } catch (IndexOutOfBoundsException e) {
                        result.put("result", "1");
                        result.put("msg", "删除成功");
                        session.removeAttribute("grade");
                        session.removeAttribute("classes");
                        session.removeAttribute("profession");
                        session.removeAttribute("college");
                        JSONObject object = new JSONObject(result);
                        return object;
                    }
                case "3":
                    try {
                        while (true) {
                            OtherEmployeeInfo otherEmployeeInfo = otherEmployeeInfoRepository.findEmployeeGroup(department).get(0);
                            accountRepository.deleteByStuempno(otherEmployeeInfo.getEmpno());
                        }
                    } catch (IndexOutOfBoundsException e) {
                        result.put("result", "1");
                        result.put("msg", "删除成功");
                        session.removeAttribute("department");
                        JSONObject object = new JSONObject(result);
                        return object;
                    }
            }
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "一股神秘力量阻止你的操作，请稍后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("logOut")
    public Object logOut(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);
        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        result.put("result", "1");
        Account account = accountRepository.findByStuempno(no).get(0);
        session.invalidate();
        account.setSessionid(null);
        accountRepository.save(account);
        result.put("msg", "用户：" + account.getName() + "注销成功，期待您的下次使用");
        JSONObject object = new JSONObject(result);
        return object;
    }

    public boolean forceLogOut(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        boolean status;
        try {
            if (session == null) {
                status = false;
                return status;
            }
            if (!session.getId().equals(accountRepository.findByStuempno((String) session.getAttribute("no")).get(0).getSessionid())) {
                status = true;
                session.invalidate();
            } else {
                status = false;
            }
        } catch (Exception e) {
//        } catch (NullPointerException e) {
            status = true;
            session.invalidate();
            return status;
        }
        return status;
    }

}